﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace QLCH_ANUONG.Forms
{
    public partial class frmBaoCaoThang : Form
    {
        public frmBaoCaoThang()
        {
            InitializeComponent();
        }
        private string strMaso = "";
        private void button1_Click(object sender, EventArgs e)
        {
            string strNgay = dateTimePicker.Value.Day + "/" + dateTimePicker.Value.Month + "/" + dateTimePicker.Value.Year;
            DateTime Ngay = Convert.ToDateTime(strNgay);
            strNgay = Ngay.ToString("yyyy/MM/dd");
            string strSQL = "ALTER proc [dbo].[proc_load_baocaotongketchitiet] as exec proc_load_baocaochitiet " + "'" +strNgay+ "'";
            clsMain.intBaoCaoThang = dateTimePicker.Value.Month;
            ADODB.Recordset Rs = new ADODB.Recordset();
            Rs.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);

            frmBaoCaoChiTiet frm = new frmBaoCaoChiTiet();
            frm.Show();
        }

        private void frmBaoCaoThang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dtsQLAU.proc_load_baocaoluongthang' table. You can move, or remove it, as needed.
            this.proc_load_baocaoluongthangTableAdapter.Fill(this.dtsQLAU.proc_load_baocaoluongthang);
            // TODO: This line of code loads data into the 'bAOCAOLUONGTHANG.proc_load_baocaoluongthang' table. You can move, or remove it, as needed.
           // this.proc_load_baocaoluongthangTableAdapter2.Fill(this.bAOCAOLUONGTHANG.proc_load_baocaoluongthang);
            string strTag = Convert.ToString(this.Tag);
            clsMain.flexConn.InitForms(strTag, ref fctxtCode);
            label1.Visible = false;
            txtTen.Visible = false;
            label2.Visible = false;
            txtTienThuong.Visible = false;
            butCapNhat.Visible = false;
            butCapNhat.Enabled = false;
            RefreshGirdControl();
        }
        private void RefreshGirdControl()
        {
            try
            {
                string strNgay = dateTimePicker.Value.Day + "/" + dateTimePicker.Value.Month + "/" + dateTimePicker.Value.Year;
                DateTime Ngay = Convert.ToDateTime(strNgay);
                strNgay = Ngay.ToString("yyyy/MM/dd");
                string strSQL = "ALTER proc [dbo].[proc_load_baocaoluongthang] as exec proc_load_baocaoluong '" + strNgay + "'";
                clsMain.intBaoCaoThang = dateTimePicker.Value.Month;
                ADODB.Recordset Rs = new ADODB.Recordset();
                Rs.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                this.proc_load_baocaoluongthangTableAdapter.Fill(this.dtsQLAU.proc_load_baocaoluongthang);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Refress Girdcontrol\n" + ex.ToString());
            }
        }
        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                RefreshGirdControl();
                butTinhLuong.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Date change value\n" + ex.ToString());
            }
        }
        private int intRow = 100;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            intRow = gridView1.FocusedRowHandle;
            
        }



        private void butXemLuong_CheckedChanged(object sender, EventArgs e)
        {
            if (butXemLuong.Checked == true)
            {
                label1.Visible = false;
                txtTen.Visible = false;
                label2.Visible = false;
                txtTienThuong.Visible = false;
                txtMaso.Visible = false;
                lblMaso.Visible = false;
                gridControl1.Visible = true;
                butTinhLuong.Checked = false;
            }
            else 
            {

            }

        }
        private string strThang = "";
        private void butTinhLuong_CheckedChanged(object sender, EventArgs e)
        {
            if (butTinhLuong.Checked == true)
            {
                if (dateTimePicker.Value <= DateTime.Now)
                {
                    label1.Visible = true;
                    txtTen.Visible = true;
                    label2.Visible = true;
                    txtTienThuong.Visible = true;
                    butCapNhat.Visible = true;
                    butXemLuong.Checked = false;
                    lblMaso.Visible = true;
                    txtMaso.Visible = true;
                    string strNgay = dateTimePicker.Value.Day + "/" + dateTimePicker.Value.Month + "/" + dateTimePicker.Value.Year;
                    DateTime Ngay = Convert.ToDateTime(strNgay);
                    strNgay = Ngay.ToString("yyyy/MM/dd");
                    strThang = dateTimePicker.Value.Month.ToString();
                    SaveDataLuongThang(strNgay, 1);
                }
                else
                {
                    MessageBox.Show("Tháng bạn nhập lớn hơn tháng hiện tại");
                    label1.Visible = false;
                    txtTen.Visible = false;
                    label2.Visible = false;
                    txtTienThuong.Visible = false;
                    gridControl1.Visible = true;
                    butTinhLuong.Checked = false;
                    butCapNhat.Visible = false;
                    butCapNhat.Enabled = false;
                    lblMaso.Visible = false;
                    txtMaso.Visible = false;
                }
            }
            else
            {
                label1.Visible = false;
                txtTen.Visible = false;
                label2.Visible = false;
                txtTienThuong.Visible = false;
                gridControl1.Visible = true;
                butTinhLuong.Checked = false;
                butCapNhat.Visible = false;
                butCapNhat.Enabled = false;
                lblMaso.Visible = false;
                txtMaso.Visible = false;
            }

        }

        private bool SaveDataLuongThang(string strNgay,int State)
        {
            try
            {
                strMaso = txtMaso.Text;
                string strSQL = "select * from LUONG_THANG where datepart(year, NGAY)= datepart(year,'" + strNgay + "') and datepart(month, NGAY)= datepart(month,'" + strNgay + "') ";
                ADODB.Recordset RsExcute = new ADODB.Recordset();
                ADODB.Recordset Rs = new ADODB.Recordset();
                ADODB.Recordset RsLayLuong = new ADODB.Recordset();
                Rs.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (Rs.RecordCount > 0 && State!=2)
                {
                    if (MessageBox.Show("Tháng " + dateTimePicker.Value.Month+" đã được tính lương- Chỉ tính lương lại khi có thay đổi về lương\nBạn có muốn tính lại lương tháng "+ dateTimePicker.Value.Month, "Tính lương tháng", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return false;    
                    }
                }
                if (State == 2)
                {
                    strSQL = "exec proc_save_luongthang " + State + ",'" + strNgay + "','','" + strMaso + "'," + 0 + "," + Convert.ToInt32( txtTienThuong.Text) + "," + 0;
                    RsExcute.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                }
                else
                {
                    strSQL = "exec proc_load_baocaoluong_nonFormat '" + strNgay + "'";
                    RsLayLuong.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, -1);
                    if (RsLayLuong.RecordCount > 0)
                    {
                        for (int i = 0; i < RsLayLuong.RecordCount; i++)
                        {
                            string strMasoNhanVien = "", strTienLuong = "", strTienThuong = "", strTienThamNien = "", strCode = "";
                            clsMain.flexConn.GenerateCode(11);
                            strCode = fctxtCode.fcText;
                            strMasoNhanVien = RsLayLuong.Fields[0].Value;
                            if (Rs.RecordCount > 0)
                            {
                                Rs.MoveFirst();
                                for (int j = 0; j < Rs.RecordCount; j++)
                                {
                                    if (Rs.Fields[1].Value == strMasoNhanVien)
                                    {
                                        strCode = "";
                                        j = Rs.RecordCount;
                                    }
                                    Rs.MoveNext();
                                }
                            }
                            if (strCode != "")
                            {
                                strTienLuong = Convert.ToString(RsLayLuong.Fields[2].Value);
                                strTienThuong = "0";
                                int intTienThamNien = Convert.ToInt32(RsLayLuong.Fields[4].Value);

                                strSQL = "exec proc_save_luongthang " + State + ",'" + strNgay + "','" + strCode + "','" + strMasoNhanVien + "'," + strTienLuong + "," + strTienThuong + "," + intTienThamNien;
                                RsExcute.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                            }
                            RsLayLuong.MoveNext();
                        }
                    }
                }
                this.proc_load_baocaoluongthangTableAdapter.Fill(this.dtsQLAU.proc_load_baocaoluongthang);
                    return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Save data luong thang\n" + ex.ToString());
                return false;
            }
        }



        private void butCapNhat_Click(object sender, EventArgs e)
        {
            string strNgay = dateTimePicker.Value.Day + "/" + dateTimePicker.Value.Month + "/" + dateTimePicker.Value.Year;
            DateTime Ngay = Convert.ToDateTime(strNgay);
            strNgay = Ngay.ToString("yyyy/MM/dd");
            strThang = dateTimePicker.Value.Month.ToString();
            butCapNhat.Enabled = false;
            SaveDataLuongThang(strNgay, 2);
        }



        private void butIn_Click(object sender, EventArgs e)
        {
            xtrBangLuong xtr = new xtrBangLuong();
            
            xtr.ShowPreview();
        }

        private void txtTienThuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar!='\b')
            {
                e.Handled = true;
            }
        }

        private void txtTienThuong_TextChanged_1(object sender, EventArgs e)
        {
            butCapNhat.Enabled = true;
        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            RefreshGirdControl();
        }

        





 




        
        
    }
}
